﻿using GalaSoft.MvvmLight;
using System.Collections.ObjectModel;
using SinaWeiboClientV1;
using System.Threading.Tasks;
using System.Linq;

namespace SinaWeiboApp_WP7.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel()
        {
            ////if (IsInDesignMode)
            ////{
            ////    // Code runs in Blend --> create design time data.
            ////}
            ////else
            ////{
            ////    // Code runs "for real"
            ////}
            FriendsStatuses = new ObservableCollection<StatusesFriendsResponseArrayItem>();
            FriendsTimeline = new ObservableCollection<StatusesFriendsTimelineResponseArrayItem>();
            _sinceId = "0";
        }

        public string ScreenName
        {
            get
            {
                return App.UserInfo.ScreenName;
            }
        }

        #region 主页
        public string TitleOfMain
        {
            get
            {
                return "主页";
            }
        }

        public ObservableCollection<StatusesFriendsResponseArrayItem> FriendsStatuses { get; set; }
        public ObservableCollection<StatusesFriendsTimelineResponseArrayItem> FriendsTimeline { get; set; }

        private string _sinceId;

        public async Task LoadFriendsStatuses()
        {
            var client = new WeiboClient();
            client.CurrentAuthToken = App.Token;

            var statusFriendRequest = new StatusesFriendsRequest { Count = "20" };
            var statusFriendResponse = await client.ExecuteStatusesFriendsAsync(statusFriendRequest);
            statusFriendResponse.Value.ForEach(item => FriendsStatuses.Add(item));
            
        }

        public async Task LoadFriendsTimeline()
        {
            var client = new WeiboClient();
            client.CurrentAuthToken = App.Token;
            var friendsTimelineRequest = new StatusesFriendsTimelineRequest { Count = "20", SinceId=_sinceId };
            var friendsTimelineResponse = await client.ExecuteStatusesFriendsTimelineAsync(friendsTimelineRequest);
            FriendsTimeline.Clear();            
            friendsTimelineResponse.Value.ForEach(item =>
            {
                FriendsTimeline.Add(item);
            });
            _sinceId = ((long)friendsTimelineResponse.Value.Max(s => s.Id)).ToString();
        }
        #endregion

        #region @我的
        public string TitleOfSecend
        {
            get { return "@我的"; }
        }
        #endregion
    }
}